package com.yixin.vuehr.service;


import com.yixin.vuehr.bean.RespPageBean;
import com.yixin.vuehr.mapper.EmployeeMapper;
import com.yixin.vuehr.model.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeService {
    public static final Logger logger = LoggerFactory.getLogger(EmployeeService.class);
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    public RespPageBean getEmployeeByPage(Integer page, Integer size, Employee employee, Date[] bDateScope) {
        if (page!=null && size!=null){
            page=(page-1)*size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(page,size,employee,bDateScope);
        Long total = employeeMapper.getTotal(employee,bDateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    public Integer addEmp(Employee employee) {
        int result = employeeMapper.insertSelective(employee);
        if (result==1){
            Employee emp = employeeMapper.getEmployeeById(employee.getId());
            logger.info(emp.toString());
            rabbitTemplate.convertAndSend("yixin.mail.welcome",emp);
        }
        return result;
    }

    public Integer maxWorkId() {
        return employeeMapper.maxWorkId();
    }

    public Integer deleteEmpById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    public Integer updateEmp(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public Integer addEmps(List<Employee> list) {
        return employeeMapper.addEmps(list);
    }

    public RespPageBean getEmployeeByPageWithSalary(Integer page, Integer size) {
        if (page!=null && size!=null){
            page=(page-1)*size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPageWithSalary(page,size);
        Long total = employeeMapper.getTotal(null,null);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    public Integer updateEmployeeSalaryById(Integer eid, Integer sid) {
        return employeeMapper.updateEmployeeSalaryById(eid,sid);
    }
}
